#ifndef SOKO_BYTE_ARRAY_REP_H
#define SOKO_BYTE_ARRAY_REP_H

#include <assert.h>
#include <soko/RetainableObject.h>


class ByteArray::ByteArrayRep : public RetainableObject<ByteArrayRep> {

private:
	std::vector<unsigned char> vector;

public:
	ByteArrayRep(int capacity) : vector(capacity) {
	}

	ByteArrayRep(const unsigned char * bytes, int length) : vector(bytes, bytes + length) {
	}

	int length() const {
		return vector.size();
	}

	int capacity() const {
		return vector.capacity();
	}

	const unsigned char * bytes() const {
		const unsigned char * bytes = 0;
		if (length() > 0) {
			bytes = &vector[0];
		}
		return bytes;
	}
	
	unsigned char & operator[](int index) {
		assert(vector.size() > 0);
		assert(index >= 0 && index < vector.size());
		return *(&vector[index]);
	}

	void copyTo(int offset, unsigned char * buffer, int length) {
		assert(offset >= 0 && length >= 0 && buffer != 0);
		assert(this->length() > offset);

		if (offset + length > this->length()) {
			length = this->length() - offset;
		}
		unsigned char * src = &vector[offset];
		for (; length > 0; --length) {
			*buffer++ = *src++;
		}
	}

	void append(unsigned char aByte) {
		vector.push_back(aByte);
	}

	void append(const unsigned char * bytes, int length) {
		vector.insert(vector.end(), bytes, bytes + length);
	}

	
};

#endif // SOKO_BYTE_ARRAY_REP_H
